package org.asiainfo.flowable.controller;

import org.asiainfo.flowable.domain.bo.WfDeployFormBo;
import org.asiainfo.flowable.service.WfDeployFormService;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.flowable.service.WfFormService;
import org.asiainfo.flowable.domain.vo.WfFormVo;
import org.asiainfo.flowable.domain.bo.WfFormBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;

import java.util.List;

/**
 * 流程表单信息表
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wfForm")
public class WfFormController extends BaseController {

    private final WfFormService wfFormService;

    private final WfDeployFormService wfDeployFormService;

    /**
     * 分页查询
     *
     * @param wfFormBo  筛选条件
     * @param pageQuery 分页对象
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo<WfFormVo> list(WfFormBo wfFormBo, PageQuery pageQuery) {
        return wfFormService.queryPageList(wfFormBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param formId 主键
     * @return 单条数据
     */
    @GetMapping("/{formId}")
    public R<WfFormVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("formId") Long formId) {
        return R.ok(wfFormService.queryById(formId));
    }

    /**
     * 新增数据
     *
     * @param wfFormBo 实体
     * @return 新增结果
     */
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WfFormBo wfFormBo) {
        if (wfFormService.checkWfFormNameUnique(wfFormBo)) {
            return R.fail("新增表单" + wfFormBo.getFormName() + "失败，业务名称已存在");
        }
        return toAjax(wfFormService.insertByBo(wfFormBo));
    }

    /**
     * 编辑数据
     *
     * @param wfFormBo 实体
     * @return 编辑结果
     */
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WfFormBo wfFormBo) {
        if (wfFormService.checkWfFormNameUnique(wfFormBo)) {
            return R.fail("修改表单" + wfFormBo.getFormName() + "失败，业务名称已存在");
        }
        return toAjax(wfFormService.updateByBo(wfFormBo));
    }

    /**
     * 删除数据
     *
     * @param formIds 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/{formIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] formIds) {
        return toAjax(wfFormService.deleteWithValidByIds(List.of(formIds)));
    }

    /**
     * 挂载流程表单
     *
     * @param wfDeployFormBo 流程实例关联表单
     * @return 挂载结果
     */
    @PostMapping("/addDeployForm")
    public R<Void> addDeployForm(@RequestBody WfDeployFormBo wfDeployFormBo) {
        return toAjax(wfDeployFormService.insertByBo(wfDeployFormBo));
    }
}

